﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class controls_Password : System.Web.UI.UserControl
{
    private string _validationGroup;
    public string ValidationGroup
    {
        get { return this._validationGroup; }
        set { this._validationGroup = value; }
    }

    private string _textBoxCssClass;
    public string TextBoxCssClass
    {
        get { return this._textBoxCssClass; }
        set { this._textBoxCssClass = value; }
    }

    private string _titleCssClass;
    public string TitleCssClass
    {
        get { return this._titleCssClass; }
        set { this._titleCssClass = value; }
    }

    private string _passwordCssClass;
    public string PasswordCssClass
    {
        get { return this._passwordCssClass; }
        set { this._passwordCssClass = value; }
    }

    private string _passwordStrengthCssClass;
    public string PasswordStrengthCssClass
    {
        get { return this._passwordStrengthCssClass; }
        set { this._passwordStrengthCssClass = value; }
    }

    private string _passwordErrorMessageCssClass;
    public string PasswordErrorMessageCssClass
    {
        get { return this._passwordErrorMessageCssClass; }
        set { this._passwordErrorMessageCssClass = value; }
    }

    private string _passwordCompareCssClass;
    public string PasswordCompareCssClass
    {
        get { return this._passwordCompareCssClass; }
        set { this._passwordCompareCssClass = value; }
    }

    private string _passwordCompareErrorMessageCssClass;
    public string PasswordCompareErrorMessageCssClass
    {
        get { return this._passwordCompareErrorMessageCssClass; }
        set { this._passwordCompareErrorMessageCssClass = value; }
    }

    private string _passwordHintCssClass;
    public string PasswordHintCssClass
    {
        get { return this._passwordHintCssClass; }
        set { this._passwordHintCssClass = value; }
    }

    public int Strength
    {
        get { return CalculateStrength(tbxPassword.Text); }
    }

    public string Password
    {
        get { return this.tbxPassword.Text; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this._validationGroup))
        {
            revPasswordValidator.ValidationGroup = this._validationGroup;
            cmvComparePassword.ValidationGroup = this._validationGroup;
            cuvPassword.ValidationGroup = this._validationGroup;
            rqvPassword.ValidationGroup = this._validationGroup;
        }

        if (!string.IsNullOrEmpty(this._textBoxCssClass))
        {
            tbxPassword.CssClass = this._textBoxCssClass;
            tbxConfirmPassword.CssClass = this._textBoxCssClass;
        }  
    }

    protected void validatePasswordStrength(object sender, ServerValidateEventArgs e)
    {
        e.IsValid = false;
        if (!string.IsNullOrEmpty(tbxPassword.Text))
        {
            int strength = CalculateStrength(tbxPassword.Text);

            if (strength > 10)
                e.IsValid = true;
        }
        else
            e.IsValid = true;
    }

    private int CalculateStrength(string password)
    {
        int strength = 0;
        int symbolTypeCount = 0;
        int lowerTypeCount = 0;
        int upperTypeCount = 0;
        int numberTypeCount = 0;
        int typeCount = 0;

        char[] brokenPassword = password.ToCharArray();
        int[] frequencyCounter = new int[257];

        for (int i = 0; i < brokenPassword.Length; i++)
        {
            int value = Convert.ToInt32(brokenPassword[i]);
            
            if (value < 256)
                frequencyCounter[value]++;
            else
                frequencyCounter[256]++;

            if (char.IsSymbol(brokenPassword[i]))
                symbolTypeCount++;
            else if (char.IsNumber(brokenPassword[i]))
                numberTypeCount++;
            else if (char.IsLower(brokenPassword[i]))
                lowerTypeCount++;
            else if (char.IsUpper(brokenPassword[i]))
                upperTypeCount++;    
        }

        int uniqueCount = 0;
        for (int j = 0; j < 257; j++)
        {
            if (frequencyCounter[j] > 0)
                uniqueCount++;
        }

        if (symbolTypeCount > 0)
            typeCount++;
        if (numberTypeCount > 0)
            typeCount++;
        if (upperTypeCount > 0)
            typeCount++;
        if (lowerTypeCount > 0)
            typeCount++;

        strength = typeCount * uniqueCount;

        return strength;
    }
}
